<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('编辑')" :v-if="dialogVisible" :visible.sync="dialogVisible" :width="lang === 'zh'?'600px':'720px'" top="10vh" @close='close'>
    <div class="dialogContentBoxMaxHeight80">
      <el-form ref="form" :model="record" :label-width="lang === 'zh'?'130px':'240px'" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('日志监控策略状态')" prop="state">
          <el-radio-group v-model="record.state">
            <el-radio label="enable" class="enable">{{$t('启用')}}</el-radio>
            <el-radio label="disable" class="disable">{{$t('禁用')}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('日志模块')" prop="state">
          {{ $t(modeNameArray[record.name]) }}
        </el-form-item>
        <el-form-item :label="$t('日志告警级别')" prop="level">
          <el-radio-group v-model="record.level" :disabled='record.state === "disable"'>
            <el-radio label="debug" class="debug">DEBUG</el-radio>
            <el-radio label="info" class="info">INFO</el-radio>
            <el-radio label="alert" class="alert">ALERT</el-radio>
            <el-radio label="error" class="error">ERROR</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('告警声音开关')" prop="voice">
          <el-radio-group v-model="record.voice" :disabled='record.state === "disable"'>
            <el-radio label="on" class="on">{{$t('开')}}</el-radio>
            <el-radio label="off" class="off">{{$t('关')}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('告警信息发送方式')" prop="method">
          <el-radio-group v-model="record.method" :disabled='record.state === "disable"'>
            <el-radio label="none" class="none">{{$t('无')}}</el-radio>
            <el-radio label="smtp" class="smtp">SMTP</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('告警接收者')" prop="reciver">
          <el-input v-emoji v-model="record.reciver" ref="reciver" :disabled='record.state === "disable" || record.method === "none"' style="width:272px;" size='small'></el-input>
          <el-checkbox v-model="ai" style="margin-left:10px;" v-if='record.state === "enable" && record.method === "smtp"'>{{$t('智能关联')}}</el-checkbox>
        </el-form-item>
        <el-form-item :label="$t('告警时间间隔（秒）')" prop="interval" v-if="record.name !== 'log-manager' && record.name !== 'interface'">
          <el-input v-emoji v-model="record.interval" ref="interval" :disabled='record.state === "disable"' style="width:272px;" size='small'></el-input>
          <el-tooltip class="item" effect="light" :content="$t('相同信息告警时间间隔')" placement="top">
            <i class="el-icon-warning-outline" style="color:#E6A23C;font-size:16px;"></i>
          </el-tooltip>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { setLogAlert } from '@/api/system/alarm/strategy'
import { inputClass } from '@/utils/input-class.js'
export default {
  name: 'LogDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    var validateReciver = (rule, value, callback) => {
      let email = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/
      if (value === '') {
        callback()
      } else if (email.test(value)) {
        callback()
      } else {
        callback(new Error(this.$t('请输入邮箱格式')))
      }
    }
    return {
      dialogVisible: false,
      addApiDialogVisible: false,
      lang: this.$i18n.locale,
      ai: false,
      record: {
        name: '',
        state: 'disable',
        level: 'debug',
        voice: 'off',
        method: 'none',
        reciver: '',
        interval: ''
      },
      recordCopy: {
        name: '',
        state: 'disable',
        level: 'debug',
        voice: 'off',
        method: 'none',
        reciver: '',
        interval: ''
      },
      modeNameArray: {
        'access-control': '访问控制',
        'attack-defense': '异常报文攻击防御',
        'ddos-defense': 'FLOOD攻击防御',
        'ips': '入侵防御',
        'content-filter': '深度过滤策略',
        'av': '病毒过滤',
        'url-filter': 'URL过滤',
        'mgmt-defense': '自身防护'
      },
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }],
        state: [{ required: false, validator: validateName, trigger: 'blur' }],
        level: [{ required: false, validator: validateName, trigger: 'blur' }],
        voice: [{ required: false, validator: validateName, trigger: 'blur' }],
        method: [{ required: false, validator: validateName, trigger: 'blur' }],
        reciver: [{ required: false, validator: validateReciver, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['reciver', 'interval']
        inputClass(this.$refs, data)
      })
      if (data) {
        this.record.name = data['mode-name']
        this.record.state = data.state
        this.record.level = data.level
        this.record.voice = data['alarm-voice']
        this.record.reciver = data.reciver ? data.reciver : ''
        this.record.interval = data.interval ? data.interval : ''
        if (data['alarm-method'] === 'ai-smtp') {
          this.record.method = 'smtp'
          this.ai = true
        } else {
          this.record.method = data['alarm-method']
        }
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let form = JSON.parse(JSON.stringify(this.record))
          if (form.method === 'smtp' && this.ai) {
            form.method = 'ai-smtp'
          }
          setLogAlert(form).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t(res.msg),
                type: 'success'
              })
              this.close()
              this.$parent.listData()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.ai = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    }
  }
}
</script>
